import { Controller, Post, Files } from '@midwayjs/core';
import { UploadFileInfo } from '@midwayjs/busboy';
import { ApiBody, ApiProperty, BodyContentType } from '@midwayjs/swagger';
import { copyFile } from 'fs/promises';
import { generateUniqueString, renameFile } from '../commen/tool';

export class CreateCatDto {
  // ...
  @ApiProperty({
    type: 'array',
    items: {
      type: 'string',
      format: 'binary',
    },
  })
  files: any;
}
@Controller('/')
export class HomeController {
  @Post('/upload' /*...*/)
  @ApiBody({
    contentType: BodyContentType.Multipart,
    schema: {
      type: CreateCatDto,
    },
  })
  async upload(
    @Files() files: Array<UploadFileInfo>
    // @Fields() fields: Record<string, string>
  ) {
    files.forEach(file => {
      const name = generateUniqueString();
      file.filename = renameFile(file.filename, String(name));
    });
    for (let i = 0; i < files.length; i++) {
      await copyFile(files[i].data, 'C:/midway/file/' + files[i].filename);
    }
    return files;
  }
}
